package cn.wen.base.image.glide;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import cn.wen.base.image.IImageEngine;


/**
 * Created by Administrator on 2017/6/19.
 */

public class GlideEngine implements IImageEngine {

    public static final int WIDTH = 480;
    public static final int HEIGHT = 800;
    public static final float SIZE_MULTIPLIER = 0.1f;// 模糊加载
    public static final int DURATION = 200;//动画时长

    private RequestOptions mOptions = new RequestOptions()
            .fitCenter()
            .override(WIDTH, HEIGHT)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
    private RequestOptions mOptions2 = new RequestOptions()
            .fitCenter()
            .override(WIDTH, HEIGHT)
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);

    @Override
    public void displayImage(Context context, ImageView imageView, String imageUrl, int errorResId) {
        mOptions.placeholder(errorResId).error(errorResId);
        displayImage(context, imageView, imageUrl, mOptions);
    }

    private void displayImage(Context context, ImageView imageView, String imageUrl, RequestOptions options) {
        Glide.with(context)
                .load(imageUrl)
                .apply(options)
                .thumbnail(SIZE_MULTIPLIER)
                .into(imageView);
    }

    @Override
    public void displayImage(Context context, ImageView imageView, String imageUrl) {
        displayImage(context,imageView,imageUrl,mOptions2);
    }

    @Override
    public void displayImage(Context context, ImageView imageView, int resId) {
        Glide.with(context)
                .asFile()
                .thumbnail(SIZE_MULTIPLIER)
                .load(resId)
                .into(imageView);
    }

    @Override
    public void displayCircleImage(Context context, ImageView imageView, String imageUrl, int errorResId) {
        mOptions.placeholder(errorResId).error(errorResId);
        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .thumbnail(SIZE_MULTIPLIER)
                .apply(mOptions)
                .into(new CircularViewTarget(context, imageView));
    }

    @Override
    public void displayRadiusImage(Context context, ImageView imageView, String imageUrl, int errorResId, float radius) {
        mOptions.placeholder(errorResId).error(errorResId);
        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .thumbnail(SIZE_MULTIPLIER)
                .apply(mOptions)
                .into(new RadiusViewTarget(context, imageView, radius));
    }

    @Override
    public void clear(Context context) {
        Glide.get(context).clearMemory();
    }
}
